package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;

import javax.swing.Icon;

import com.abigdreamer.im.server.module.EIMServer;
import com.abigdreamer.im.server.module.msgProcessConfig.MessageProcessConfig;
import com.abigdreamer.im.server.serverConfig.EIMServerConfig;
import com.abigdreamer.im.server.tools.ServerUtils;
import com.abigdreamer.im.server.ui.desktop.DeskTopConfig;
import com.abigdreamer.im.server.ui.desktop.EIMDesktop;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;
import com.thoughtworks.xstream.XStream;

public class ConfigFrame extends DeskTopFrame{

	
	private ConfigWindow configWindow;
	

	
	public ConfigFrame(){
		super("配置信息", true);
		initComponents();
		this.setClosable(true);
		this.setBounds(100, 40, 550, 450);
	}
	
	 private void initComponents() {
		    configWindow =new ConfigWindow();
		    this.getContentPane().add(configWindow,BorderLayout.CENTER);
	    }
	
}


class ConfigWindow extends EIMWindow{
	
	public ConfigWindow(){
		
		EIMServer server=EIMServer.getInstance();
		EIMServerConfig config=server.getConfig();
		ConfigWindowItem serverConfigItem=new ConfigWindowItem();
		serverConfigItem.setWindow(this);
		Icon icon=ResourseMannager.getIconByExtractor("xml", true);
		serverConfigItem.setConfig(config);
		serverConfigItem.setIcon(icon);
		serverConfigItem.setText("服务器配信息");
		serverConfigItem.setToolTipText("双击可打开查看服务器配信息");
		this.add(serverConfigItem, 1);
		
		EIMDesktop desktop=EIMDesktop.getInstance();
		DeskTopConfig deskTopConfig=desktop.getDeskTopConfig();
		ConfigWindowItem deskTopConfiItem=new ConfigWindowItem();
		deskTopConfiItem.setWindow(this);
		deskTopConfiItem.setConfig(deskTopConfig);
		Icon icon2=ResourseMannager.getIconByExtractor("xml", true);
		deskTopConfiItem.setIcon(icon2);
		deskTopConfiItem.setText("桌面配信息");
		deskTopConfiItem.setToolTipText("双击可打开查看桌面配信息");
		this.add(deskTopConfiItem, 2);
		
		MessageProcessConfig processConfig=ServerUtils.getMessageProcessConfig();
		ConfigWindowItem msgProcessConfiItem=new ConfigWindowItem();
		msgProcessConfiItem.setWindow(this);
		msgProcessConfiItem.setConfig(processConfig);
		Icon icon3=ResourseMannager.getIconByExtractor("xml", true);
		msgProcessConfiItem.setIcon(icon3);
		msgProcessConfiItem.setText("服务器消息处理器配信息");
		msgProcessConfiItem.setToolTipText("双击可打开查看服务器消息处理器配信息");
		this.add(msgProcessConfiItem, 3);
		
	}
	
}

class ConfigWindowItem extends WindowItem{
	Object config=null;
	public void mouseClicked(MouseEvent e) {
		super.mouseClicked(e);
		String xml="";
		if(e.getClickCount()==2){
			if(config instanceof EIMServerConfig ){
				XStream xstream	=ServerUtils.getProcessServserConfigXStream();
				xml=xstream.toXML(config);
			}else if(config instanceof DeskTopConfig){
				XStream xstream	=ServerUtils.getDeskTopConfigXStream();
				xml=xstream.toXML(config);
			}
			else if(config instanceof MessageProcessConfig){
				XStream xstream	=ServerUtils.getProcessConfigXStream();
				xml=xstream.toXML(config);
			}
			ConfigEdit configEdit=new ConfigEdit("配置信息",xml,true);
			configEdit.setConfig(config);
			EIMDesktop desktop=EIMDesktop.getInstance();
			configEdit.setClosable(true);
			configEdit.setBounds(50, 50, 700, 300);
			desktop.add(configEdit, new Integer(10));
			configEdit.show();
		}
	}
	public Object getConfig() {
		return config;
	}
	public void setConfig(Object config) {
		this.config = config;
	}
}


